package com.zhdl.network.prefiretcp.after;

import cn.hutool.core.util.HexUtil;
import com.project.command.instruction.strategy.abs.AbsAfterControlCommand;
import com.project.command.instruction.strategy.process.data.AfterStrategyParam;
import com.zhdl.common.eventbus.center.EventBusCenter;
import com.zhdl.common.eventbus.event.PreFireEvent;
import com.zhdl.common.ytools.ByteHelper;
import com.zhdl.network.prefiretcp.dto.PreFireTcpDto;
import io.netty.buffer.ByteBuf;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service(PrefireTcpAfterService.PREFIRE_TCP_AFTER_SERVICE)
public class PrefireTcpAfterService extends AbsAfterControlCommand<PreFireTcpDto, ByteBuf> {
    public static final String PREFIRE_TCP_AFTER_SERVICE = "preFireTcpAfterService";

    /**
     * 后置处理结果判断
     *
     * @param resp
     * @return
     */
    @Override
    public boolean afterResultJudge(AfterStrategyParam resp) {
        List<byte[]> bytes = resp.getNewData();
        byte[] bytes1 = bytes.get(0);
        String s = HexUtil.encodeHexStr(bytes1);
        System.out.println("After当前接收数据为:------->" + s);

        PreFireEvent preFireEvent = new PreFireEvent();
        preFireEvent.setType("tcp");
        preFireEvent.setPort(resp.getPortKey());
        preFireEvent.setEvent(resp);
        EventBusCenter.getEventBus().post(preFireEvent);
        log.info("After tcp:----" + resp.getPortKey() + "发送指令>>>>" + resp.getInstruction() + "收到指令》》》》" + s);
        return true;
    }

    /**
     * 判断 读取 3200 状态字数据
     */
    public String getMotorStatus(PreFireTcpDto PreFireTcpDto) {
        byte[] unitBuff = PreFireTcpDto.getUnitBuff();
        return HexUtil.encodeHexStr(unitBuff);
    }

}
